package com.situ.mall.controller;


import com.situ.mall.pojo.Category;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-06-17
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    //1.使用Map实现缓存
    //缺点：1、在内存里面，重启就销毁  2、只能在单机下面有效，无法实现分布式环境下缓存
    private Map<String, List<Category>> map = new HashMap<>();

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/selectTopCategory")
    public Result<List<Category>> selectTopCategory() {
        System.out.println("CategoryController.selectTopCategory");
        //1.判断缓存里面有没有
        //2.如果没有，查询数据库，更新到缓存中
        List list = redisTemplate.opsForList().range("topCategoryList", 0, -1);
        if (CollectionUtils.isEmpty(list)) {
            list = categoryService.selectTopCategory();
            redisTemplate.opsForList().rightPushAll("topCategoryList", list);
        }
        return Result.ok(list);
    }

    @RequestMapping("/selectSecondCategoryByTopCategoryId")
    public Result<List<Category>> selectSecondCategoryByTopCategoryId(Integer id) {
        //soutm
        System.out.println("CategoryController.selectSecondCategoryByTopCategoryId");
        List list = redisTemplate.opsForList().range("secondCategoryList:" + id, 0, -1);
        if (CollectionUtils.isEmpty(list)) {
            list = categoryService.selectSecondCategoryByTopCategoryId(id);
            redisTemplate.opsForList().rightPushAll("secondCategoryList:" + id, list);
        }
        return Result.ok(list);
    }

    @RequestMapping("/selectTopCategory1")
    public Result<List<Category>> selectTopCategory1() {
        System.out.println("CategoryController.selectTopCategory");
        //1.判断缓存里面有没有
        //2.如果没有，查询数据库，更新到缓存中
        List<Category> list = map.get("topCategoryList");
        if (CollectionUtils.isEmpty(list)) {
            list = categoryService.selectTopCategory();
            map.put("topCategoryList", list);
        }
        return Result.ok(list);
    }

    @RequestMapping("/selectSecondCategoryByTopCategoryId1")
    public Result<List<Category>> selectSecondCategoryByTopCategoryId1(Integer id) {
        //soutm
        System.out.println("CategoryController.selectSecondCategoryByTopCategoryId");
        List<Category> list = map.get("secondCategoryList:" + id);
        if (CollectionUtils.isEmpty(list)) {
            list = categoryService.selectSecondCategoryByTopCategoryId(id);
            map.put("secondCategoryList:" + id, list);
        }
        return Result.ok(list);
    }
}

